package com.joysw.ui.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.google.gson.Gson;
import com.joysw.R;
import com.joysw.models.FxModel;
import com.joysw.models.FxModelMsg;
import com.joysw.ui.FoodsDetailActivity;
import com.joysw.ui.FxDetailActivity;
import com.joysw.view.TitleBarView;
import com.nostra13.universalimageloader.core.ImageLoader;

/**   
 * @Title: FxFragment.java 
 * @Package com.joysw.ui.fragment 
 * @Description: 发现页面
 * @author LIMINGYAN  
 * @date 2015年3月28日 下午9:29:05 
 * @version V1.0   
 */
public class FxFragment extends BaseFragment{
	private static final String TAG = "FxFragment";
	private Context mContext;
	private View mBaseView;
	
	@InjectView(R.id.fx_title_bar)
	TitleBarView mTitleBar;
	
	@InjectView(R.id.fx_list)
	ListView mListView;

	private MyListAdapter mListAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContext = getActivity();
		mBaseView = inflater.inflate(R.layout.fragment_fx, container,
				false);
		ButterKnife.inject(this, mBaseView);
		initView();
		
		new MyTask().execute();
		return mBaseView;
	}
	
	private void initView(){
		mTitleBar.setCommonTitle(View.GONE, View.VISIBLE, View.GONE);
		mTitleBar.setTitleText(R.string.fragment_fx_title);
	}
	
	class MyTask extends AsyncTask<Void, Void, FxModelMsg> {

		@Override
		protected FxModelMsg doInBackground(Void... params) {
			String json = "{\"code\":200,\"msg\":\"ok\",list:["
					+ "{\"id\":110,\"imgeView\":\"http://pic23.nipic.com/20120819/1147326_174431669102_2.jpg\",\"content\":\"职业服装,时尚服装相关图片\",\"price\":\"\",\"jianyiPrice\":\"\"},"
					+ "{\"id\":110,\"imgeView\":\"http://pic13.nipic.com/20110309/4615144_164253641121_2.jpg\",\"content\":\"服装模特摄影\",\"price\":\"\",\"jianyiPrice\":\"\"},"
					+ "{\"id\":110,\"imgeView\":\"http://pic12.nipic.com/20110110/1681881_170827366000_2.jpg\",\"content\":\"Cbsi中国·PChome.net\",\"price\":\"\",\"jianyiPrice\":\"\"}"
					+"]}";
			Gson gson = new Gson();
			FxModelMsg msg = gson.fromJson(json, FxModelMsg.class);
			return msg;
		}

		@Override
		protected void onPostExecute(FxModelMsg result) {
			mListAdapter = new MyListAdapter(result.list);
			mListView.setAdapter(mListAdapter);
		}
	}
	
	public class MyListAdapter extends BaseAdapter{
		private ArrayList<FxModel> mList;
		private LayoutInflater mInflater;
		
		public MyListAdapter(ArrayList<FxModel> list) {
			// TODO Auto-generated constructor stub
			mInflater = LayoutInflater.from(getActivity());
			mList = list;
		}
		@Override
		public int getCount() {
			return mList==null?0:mList.size();
		}

		@Override
		public FxModel getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return getItem(position).id;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.list_item_fx, null);
				holder.rlImage=(RelativeLayout)convertView.findViewById(R.id.inc_image);
				holder.imageView = (ImageView) holder.rlImage.findViewById(R.id.item_image);
				holder.context=(TextView) holder.rlImage.findViewById(R.id.item_description);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(FxFragment.this.getActivity(),FxDetailActivity.class);
					startActivity(intent);
				}
			});
			
			final FxModel bean = getItem(position);
			ImageLoader.getInstance().displayImage(bean.imgeView, holder.imageView);
			holder.context.setText(bean.content);
			return convertView;
		}
	}
	
	private static class ViewHolder {
		public TextView context;//描述
		public ImageView imageView;//展示图片
		public RelativeLayout rlImage;//图片位置
		public TextView price;//价格
		public TextView jianyi_price;//建议价格
	}
}
